<?php

class Category {
	private $id;
	private $description;
	
	function __construct($id,$description){
		$this -> id = $id;
		$this -> description = $description;
	}
	
	
	function getCategory($db,$id){
		$result=$db->query("SELECT * FROM category WHERE cat_IDcategory = $id",2);
		$category=new Category($result[0]['cat_IDcategory'],
						$result[0]['cat_description']);
				
		return $category;
	
	}
	
	function getList($db){
		$result=$db->query("SELECT * FROM category",2);
		return $result;
	}
	
	function insert($db,$category){
		$description	= $category -> getDescription();
		$result=$db->insert("INSERT INTO category (cat_description) VALUES('".addslashes(utf8_decode($description))."')");
		return $result;
	}	
	
	function delete($db,$category){
		$id 			= $category -> getID();
		$result=$db->delete("DELETE FROM subcategory WHERE sub_id_category = " . $id);
		if($result == 1){
			$result=$db->delete("DELETE FROM category WHERE cat_IDcategory = " . $id);
		}
		return $result;
	}	
	
	function update($db,$category){
		$id 			= $category -> getID();
		$description	= $category -> getDescription();
		$result=$db->update("UPDATE category SET cat_description='".addslashes(utf8_decode($description))."' WHERE cat_IDcategory = " . $id);
		return $result;
	}		
	
	function getID(){
		return $this -> id;
	}
	
	function  setID($id){
		$this -> id = $id;
	}
	
	function getDescription(){
		return $this -> description;
	}
	
	function  setDescription($description){
		$this -> description = $description;
	}
}

?>